﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;

using Uber.HabboHotel.Misc;
using Uber.HabboHotel.Catalogs;
using Uber.HabboHotel.GameClients;
using Uber.HabboHotel.Roles;
using Uber.HabboHotel.Support;
using Uber.HabboHotel.Navigators;
using Uber.HabboHotel.Items;
using Uber.HabboHotel.Quests;
using Uber.HabboHotel.Rooms;
using Uber.HabboHotel.Rooms.Chatlogs;
using Uber.HabboHotel.Advertisements;
using Uber.HabboHotel.Achievements;
using Uber.HabboHotel.RoomBots;
using Uber.Storage;

namespace Uber.HabboHotel
{
    class Game
    {
        private GameClientManager ClientManager;
        private ModerationBanManager BanManager;
        private RoleManager RoleManager;
        private HelpTool HelpTool;
        private Catalog Catalog;
        private Navigator Navigator;
        private ItemManager ItemManager;
        private RoomManager RoomManager;
        private QuestManager QuestManager;
        private ChatlogsManager ChatlogsManager;
        private AdvertisementManager AdvertisementManager;
        private PixelManager PixelManager;
        private AchievementManager AchievementManager;
        private ModerationTool ModerationTool;
        private BotManager BotManager;
        private GameConfig GameConfiguration;
        private LowPriorityWorker LowPriorityWorker;
        private AntiMutant AntiMutant;
        private ChatWordFilter ChatWordFilter;
        private IdGenerator IdGenerator;

        public Game()
        {
            ClientManager = new GameClientManager();
            BanManager = new ModerationBanManager();
            RoleManager = new RoleManager();
            HelpTool = new HelpTool();
            Catalog = new Catalog();
            Navigator = new Navigator();
            ItemManager = new ItemManager();
            RoomManager = new RoomManager();
            QuestManager = new QuestManager();
            ChatlogsManager = new ChatlogsManager();
            AdvertisementManager = new AdvertisementManager();
            PixelManager = new PixelManager();
            AchievementManager = new AchievementManager();
            ModerationTool = new ModerationTool();
            BotManager = new BotManager();
            GameConfiguration = new GameConfig();
            LowPriorityWorker = new LowPriorityWorker();
            AntiMutant = new AntiMutant();
            ChatWordFilter = new ChatWordFilter();
            IdGenerator = new IdGenerator();

            BanManager.LoadBans();
            RoleManager.LoadRoles();
            RoleManager.LoadRights();
            HelpTool.LoadCategories();
            HelpTool.LoadTopics();
            Catalog.Initialize();
            Navigator.Initialize();
            ItemManager.LoadItems();
            RoomManager.LoadModels();
            QuestManager.InitQuests();
            AdvertisementManager.LoadRoomAdvertisements();
            AchievementManager.LoadAchievements();
            ModerationTool.LoadMessagePresets();
            ModerationTool.LoadPendingTickets();
            BotManager.LoadBots();
            BotManager.LoadPetMessages();
            ChatWordFilter.InitWords();
            IdGenerator.Initialize();

            DatabaseCleanup(1);

            PixelManager.Start();
            LowPriorityWorker.Start();
            RoomManager.StartRoomEngine();
        }

        public void DatabaseCleanup(int serverStatus)
        {
            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                dbClient.ExecuteQuery("UPDATE users SET auth_ticket = '', online = '0'");
                dbClient.ExecuteQuery("UPDATE rooms SET users_now = '0'");
                dbClient.ExecuteQuery("UPDATE user_roomvisits SET exit_timestamp = '" + UberEnvironment.GetUnixTimestamp() + "' WHERE exit_timestamp <= 0");
                dbClient.ExecuteQuery("UPDATE server_status SET status = '" + serverStatus + "', users_online = '0', rooms_loaded = '0', server_ver = '" + UberEnvironment.PrettyVersion + "', stamp = '" + UberEnvironment.GetUnixTimestamp() + "' LIMIT 1");
            }

            UberEnvironment.GetLogging().WriteLine("Database has been cleaned and reset", Uber.Core.LogLevel.Debug);
        }

        public void Destroy()
        {
            DatabaseCleanup(0);

            LowPriorityWorker.Stop();
            RoomManager.UnloadAllRooms(true);
            RoomManager.StopRoomEngine();        

            if (GetClientManager() != null)
            {
                GetClientManager().Clear();
            }

            if (GetPixelManager() != null)
            {
                PixelManager.KeepAlive = false;
                PixelManager.Stop();
            }

            UberEnvironment.GetLogging().WriteLine("Client Manager and Game has been destroyed.");
        }

        public GameClientManager GetClientManager()
        {
            return ClientManager;
        }

        public ModerationBanManager GetBanManager()
        {
            return BanManager;
        }

        public RoleManager GetRoleManager()
        {
            return RoleManager;
        }

        public HelpTool GetHelpTool()
        {
            return HelpTool;
        }

        public Catalog GetCatalog()
        {
            return Catalog;
        }

        public Navigator GetNavigator()
        {
            return Navigator;
        }

        public ItemManager GetItemManager()
        {
            return ItemManager;
        }

        public RoomManager GetRoomManager()
        {
            return RoomManager;
        }

        public QuestManager GetQuestManager()
        {
            return QuestManager;
        }

        public ChatlogsManager GetChatlogsManager()
        {
            return ChatlogsManager;
        }

        public AdvertisementManager GetAdvertisementManager()
        {
            return AdvertisementManager;
        }

        public PixelManager GetPixelManager()
        {
            return PixelManager;
        }

        public AchievementManager GetAchievementManager()
        {
            return AchievementManager;
        }

        public ModerationTool GetModerationTool()
        {
            return ModerationTool;
        }

        public ChatWordFilter GetChatWordFilter()
        {
            return ChatWordFilter;
        }

        public BotManager GetBotManager()
        {
            return BotManager;
        }

        public GameConfig GetGameConfiguration()
        {
            return GameConfiguration;
        }

        public IdGenerator GetIdGenerator()
        {
            return IdGenerator;
        }
    }

    class GameConfig
    {
        public int CreateRooms_maxRooms;
        public int CreateRooms_maxRooms_Vip;
        public int Items_Stacking_maxHeight;
        public int Motd_Enabled;
        public string Motd_Message;
        //public int Pixel_Minutes;
        //public int Pixel_Amount;

        public static string getConfEntry(string Key)
        {
            using (DatabaseClient dbClient = UberEnvironment.GetDatabase().GetClient())
            {
                return dbClient.ReadString("SELECT `val` FROM `server_config` WHERE `key` = '" + Key + "'");
            }
        }

        public GameConfig()
        {
            CreateRooms_maxRooms = int.Parse(getConfEntry("user_max_rooms"));
            CreateRooms_maxRooms_Vip = int.Parse(getConfEntry("user_max_rooms_vip"));
            Items_Stacking_maxHeight = int.Parse(getConfEntry("items_stacking_maxheight"));
            Motd_Enabled = int.Parse(getConfEntry("motd_enabled"));
            Motd_Message = getConfEntry("motd_message");
            //Pixel_Minutes = int.Parse(getConfEntry("pixel_minutes"));
            //Pixel_Amount = int.Parse(getConfEntry("pixel_amount"));
        }

        public void UpdateConfig()
        {
            CreateRooms_maxRooms = int.Parse(getConfEntry("user_max_rooms"));
            CreateRooms_maxRooms_Vip = int.Parse(getConfEntry("user_max_rooms_vip"));
            Items_Stacking_maxHeight = int.Parse(getConfEntry("items_stacking_maxheight"));
            Motd_Enabled = int.Parse(getConfEntry("motd_enabled"));
            Motd_Message = getConfEntry("motd_message");
            //Pixel_Minutes = int.Parse(getConfEntry("pixel_minutes"));
            //Pixel_Amount = int.Parse(getConfEntry("pixel_amount"));
        }
    }
}
